window.last_time = new Date().getTime();

window.is_locked = true;

function sendMessage(msg) {
	$.ajax({
		url: '/api/message',
		type: 'POST',
		data: msg,
		dataType: "json",
		success: function(data) {
			receiveMessage(data.data);
		}
	});
	$('<li>&gt;&nbsp;' + msg + '</li>').appendTo('#chat-list');
	$('#text').val('').attr('value', '');
}

function sendEvent(type) {
	$.ajax({
		url: '/api/event',
		type: 'POST',
		data: JSON.stringify({
			type: type
		}),
		dataType: "json",
		success: function(data) {
			receiveMessage(data.data);
		}
	});
}

function sendHeartbeat() {
	$.ajax({
		// url: '/api/heartbeat?idx=' + window.msg_idx,
		url: '/api/heartbeat?last_time=' + window.last_time,
		type: 'GET',
		dataType: 'json',
		success: function(data) {
			if (data.data) {
				// window.msg_idx = data.data.next_idx;
				window.last_time = data.data.last_time;
				for (var i = 0; i < data.data.msgs.length; i++) {
					receiveMessage(data.data.msgs[i]);
				};
			}
		},
		error: function(data) {
			alert('Connection failed!');
			clearInterval(window.heartbeatTimer);
		}
	});
}

function receiveMessage(msg) {
	$('<li>&lt;&nbsp;' + msg + '</li>').appendTo('#chat-list');
	console.log('< ', msg);
	if (msg.type == "authorize") {
		receiveAuthorize(msg);
	} else if (msg.type == "register") {
		receiveRegister(msg);
	}
}

function receiveAuthorize(msg) {
	if (msg.status == "ok") {
		// alert('鉴定成功！');
		switchLock();
	} else if (msg.status == "forbidden") {
		// alert('鉴定失败！');
	} else {
		// alert('发生错误！');
	}
}

function receiveRegister(msg) {
	if (msg.status == "ok") {
		// alert('录入成功！');
		$('.textbox').text("指纹录入成功！");
	} else if (msg.status == "error") {
		// alert('录入失败！');
		$('.textbox').text("指纹录入失败，请重新录入！");
	} else if (msg.status == "push") {
		// alert('请按指纹！');
		$('.textbox').text("请按下手指");
	} else if (msg.status == "remove") {
		// alert('请拿开手指！');
		$('.textbox').text("请移开手指");
	} else {
		// alert('发生错误！');
		$('.textbox').text("系统发生错误，请重试");
	}
}

function authorizeSuccess() {
}

function blink(time) {
	var speed = 200;
	var sleeptime = 200;
	var t = 0;
	function _blink_once() {
		$('.light').animate({
			opacity: 1
		},speed, function() {
			$('.light').animate({
				opacity: 0
			},speed, function() {
				t++;
				if (t < time) {
					setTimeout(_blink_once, sleeptime);
				}
			});
		});
	}
	_blink_once();
}

function switchLock() {
	window.is_locked = !window.is_locked;
	if (window.is_locked) {
		blink(1);
		$('.textbox').text("车门锁闭");
		$('.register-indicator').removeClass('active');
	} else {
		blink(2);
		$('.textbox').text("车锁已打开。可以按右边按钮 初始化/重置 指纹");
		$('.register-indicator').addClass('active');
	}
}

$('#send').click(function(event) {
	var msg = $('#text').val();
	if ((!msg) || (msg == "")) return;
	sendMessage(msg);
});
$('#register').click(function(event) {
	if (!window.is_locked) sendEvent('register');
});

$('.register').mousedown(function(event) {
	$(this).addClass('active');
});
$('.register').mouseup(function(event) {
	$(this).removeClass('active');
});

window.heartbeatTimer = setInterval(sendHeartbeat, 300);

switchLock();